package com.lqian.simplestructure.linkedlist;

/**
 * 每个节点 有前驱指针 也有后继指针
 *
 */
public class DoubleNode {
    DoubleNode pre = this;

    DoubleNode next = this;

    int data;

    public DoubleNode(int data) {
        this.data = data;
    }

    //增加一个节点(插入)
    public void after(DoubleNode node){
        //记录下该节点（正在操作的节点）的下一个节点，为了连接到加入新节点的后面
        DoubleNode nextNode = this.next;

        this.next = node;
        node.pre = this;
        node.next = nextNode;
        nextNode.pre = node;
    }

    public DoubleNode next(){
        return this.next;
    }

    public DoubleNode pre(){
        return this.pre;
    }

    public int getData() {
        return data;
    }

    public static void main(String[] args) {
        //创建节点
        DoubleNode n1 = new DoubleNode(1);
        DoubleNode n2 = new DoubleNode(2);
        DoubleNode n3 = new DoubleNode(3);
        //追加节点
        n1.after(n2);
        n2.after(n3);
        //查看上一个，自己，下一个节点的内容
        System.out.println(n2.pre().getData());
        System.out.println(n2.getData());
        System.out.println(n2.next().getData());
        System.out.println(n3.next().getData());
        System.out.println(n1.pre().getData());
    }

}
